const wxapi = require("../../../../utils/wxapi");
const { API_SNZO_FILE_URL } = require("../../../../utils/wxapi");

// pages/exchange/rank/artwork/artwork.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityIntro: null,
    detailTitle: null,
    listOfData: null,
    bodyParams: { relateActiveId: null, rank: null },
    pageParams: { pageNum: 1, pageSize: 10 },
    total: 0,
    scrollTop: 0,
    canScroll: true,
    worksImgUrl: API_SNZO_FILE_URL,
  },

  /**
   * 获取活动详情
   */
  getActivityDetail() {
    wxapi.selectArtActiveById({ id: this.data.bodyParams.relateActiveId }).then(res => {
      if (res.status === 200) {
        const detailTitle = res.data.activityName.replaceAll('"', '').split('--');
        this.setData({
          activityIntro: res.data,
          detailTitle
        });
        wx.setNavigationBarTitle({
          title: res.data.activityName
        })
      }
    })
  },

  /**
   * 获取获奖作品
   */
  getData() {
    wx.showLoading({
      title: '加载中...',
    })
    wxapi.getAwardedArtworks(this.data.bodyParams, this.data.pageParams).then(res => {
      wx.hideLoading();
      if (res.status === 200) {
        if (this.data.pageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.seachList;
        const newData = this.data.listOfData.concat(listOfData);
        this.setData({
          listOfData: newData,
          total: res.data.seachTotal,
          canScroll: true
        })
      } else {
        this.setData({
          listOfData: null,
          total: 0,
          canScroll: true
        })
      }
    })
  },

  /**
   * 加载更多
   */
  scrollToLower() {
    if (this.data.listOfData.length === this.data.total) {
      return;
    }
    this.setData({
      canScroll: false
    })
    setTimeout(() => {
      this.data.pageParams.pageNum++
      this.getData();
    }, 1000);
  },

  /**
   * 预览艺术品
   * @param {*} e 
   */
  previewWorks(e) {
    const worksInfo = e.currentTarget.dataset.works;
    wx.previewMedia({
      sources: [
        {
          url: this.data.worksImgUrl + worksInfo.showPath,
          type: 'image'
        }
      ],
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options?.relateActiveId && options.rank) {
      this.data.bodyParams.relateActiveId = options.relateActiveId;
      this.data.bodyParams.rank = options.rank;
      this.setData({
        bodyParams: this.data.bodyParams
      })
      this.getData();
      this.getActivityDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})